<?php

namespace App\Http\Controllers\Web\Seller;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use App\Repository\Models\Shop;
use App\Repository\Models\User;
use App\Repository\Repositories\Interfaces\ShopRepository;
use Illuminate\Http\Request;
use App\Repository\Models\ShopType;

/**
 * 
 *
 * @package App\Http\Controllers\Backend
 */
class ShopController extends BaseController
{
    /**
     * @var ShopRepository
     */
    protected $shop;

    protected $sp_uid;
    protected $shop_id;

    public function __construct(ShopRepository $shop)
    {
        parent::__construct();
        $this->shop = $shop;
        $this->sp_uid =array_get($this->user, 'id');
        $this->shop_id =array_get($this->shop_id, 'shop_id');
//        //用户只能操作自身所属数据
        $this->shop->scopeQuery(function ($query) {
            return $query->where('sp_uid',$this->sp_uid);

        });
    }

    public function configure()
    {
        $shops = $this->shop->with('company')->with('shopLevel')->paginate($this->perPage);

        foreach($shops['data'] as $k=> $v){

            $shops['data'][$k]['sp_type_id_arr']=explode(",",$v['sp_type_id']);
            foreach($shops['data'][$k]['sp_type_id_arr'] as $key=>$val){
                $shops['data'][$k]['sp_type_id_type'][]= ShopType::query()->find($val);
            }

        }

        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $shops, true);
    }

    public function configure_update(Request $request, $id)
    {

        $this->validate($request, [
            'sp_title' => 'required'
        ]);
        $data = $request->all();

        $ret = $this->shop->update($data, $id);

        return $this->noDataReturn($ret ? true : false);
    }

    //获取店铺信息
    public function get_shop(){

        $res=$this->shop->all();

        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $res, true);
    }

    public function item_shop(Request $request){

       $data=$request->all();
       $res=User::query()->with(['company'])->where('phone',$data['buyer_phone'])->get(['id','name','phone'])->toArray();

       return $this->success(ErrorConst::SUCCESS_CODE_MSG, $res, false);
    }


    public function seller_shop($id){

       $res=User::query()->with(['company'])->where('id',$id)->get(['id','name','phone'])->toArray();

       return $this->success(ErrorConst::SUCCESS_CODE_MSG, $res, false);
    }




}
